package com.seven.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seven.entity.Problem;
import com.seven.entity.Users;
import com.seven.entity.Worker;
import com.seven.service.ProblemService;
import com.seven.service.UsersService;
import com.seven.service.WorkerService;
import com.seven.util.PageBean;

public class ProblemServlet extends BaseServlet {

	ProblemService problemService = new ProblemService();

	/**
	 * 删除问题
	 * 
	 * @throws IOException
	 */
	public void deleteProblem(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String str = request.getParameter("problem_id");
		int problem_id = Integer.parseInt(str);

		Problem problem = new Problem();
		problem.setProblem_id(problem_id);

		int n = problemService.deleteProblem(problem);
		out.print(n);

	}

	/**
	 * 查询所有问题
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void selectAll(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		PrintWriter out = response.getWriter();
		List<Problem> list = problemService.selectAllProblem();
		JSONArray arr = getArrByList(list);
		out.print(arr);
	}

	/**
	 * 查看所有未审核的问题
	 * 
	 * @throws IOException
	 */
	public void selectProblem_isauditor0(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		List<Problem> list = problemService.selectByproblem_isauditor0();

		PrintWriter out = response.getWriter();

		JSONArray arr = getArrByList(list); // 当前页要显示的记录
		out.print(arr);
	}
	
	/**
	 * 分页查询出所有未审核的问题
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void pageQuery(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// request -> pageBean;

		List<Problem> alllist = problemService.selectByproblem_isauditor0();

		PageBean<Problem> pageBean = new PageBean<Problem>();

		String curPage = request.getParameter("curPage");
		String rowPerPage = request.getParameter("rowPerPage");
		
		
		System.out.println("asdfdafsrgsregregesrgeRG"+curPage+rowPerPage);

		pageBean.page(alllist, curPage, rowPerPage);

		PrintWriter out = response.getWriter();
		out.print(toJsonStr(pageBean));
	}

	/**
	 * 查看所有审核通过的问题
	 * 
	 * @throws IOException
	 */
	public void selectProblem_isauditor1(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		List<Problem> list = problemService.selectByproblem_isauditor1();
		JSONArray arr = getArrByList(list);
		out.print(arr);
	}

	/**
	 * 查看所有审核未通过的问题
	 * 
	 * @throws IOException
	 */
	public void selectProblem_isauditor_1(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		List<Problem> list = problemService.selectByproblem_isauditor_1();
		JSONArray arr = getArrByList(list);
		out.print(arr);
	}

	/**
	 * 查看所有已审核未处理的问题
	 * 
	 * @throws IOException
	 */
	public void selectProblem_ispass_1(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		List<Problem> list = problemService.selectByproblem_ispass_1();
		JSONArray arr = getArrByList(list);
		out.print(arr);
	}

	/**
	 * 查看所有处理中的问题
	 * 
	 * @throws IOException
	 */
	public void selectProblem_ispass0(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		List<Problem> list = problemService.selectByproblem_ispass0();
		JSONArray arr = getArrByList(list);
		out.print(arr);
	}

	/**
	 * 查看所有已处理的问题
	 * 
	 * @throws IOException
	 */
	public void selectProblem_ispass1(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		List<Problem> list = problemService.selectByproblem_ispass1();
		JSONArray arr = getArrByList(list);
		out.print(arr);
	}

	/**
	 * 传值需求:传问题id,传审核状态state=-1/0/1 更改审核状态
	 */
	public void updateProblem_isauditor(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();

		String str = request.getParameter("state");
		int state = Integer.parseInt(str);
		String str2 = request.getParameter("problem_id");
		int problem_id = Integer.parseInt(str2);

		Problem problem = new Problem();
		problem.setProblem_isauditor(state);
		problem.setProblem_id(problem_id);

		if (state == 1) {
			// 给用户加积分
			List<Problem> list = problemService.selectByProblem_id(problem);
			Problem problem2 = list.get(0);
			int users_id = problem2.getUsers_id();

			// 给用户加积分
			Users users = new Users();
			users.setUsers_id(users_id);

			UsersService usersService = new UsersService();
			usersService.addscore(users);

		}

		int n = problemService.updatePromblem(problem);

		out.print(n);
	}

	/**
	 * 传值需求:传问题id,传处理状态state=-1/0/1 更改处理状态
	 */
	public void updateProblem_ispass(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();

		String str = request.getParameter("state");
		int state = Integer.parseInt(str);
		String str2 = request.getParameter("problem_id");
		int problem_id = Integer.parseInt(str2);

		Problem problem = new Problem();
		problem.setProblem_ispass(state);
		problem.setProblem_id(problem_id);

		int n = problemService.updatePromblem2(problem);

		if (state == 1) {
			// System.out.println("8888888888888888888888888888888888888888"+state);

			List<Problem> list = problemService.selectByProblem_id(problem);
			Problem problem2 = list.get(0);
			int worker_id = problem2.getWorker_id();

			// 员工工分+1&状态改为空闲

			Worker worker = new Worker();

			worker.setWorker_id(worker_id);

			WorkerService workerService = new WorkerService();

			workerService.addScoreSetstare(worker);
		}

		out.print(n);
	}

	/**
	 * 传值要求:传员工id,传问题id 指派员工
	 */
	public void ProblemSetWorkerId(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();

		String str = request.getParameter("worker_id");
		int worker_id = Integer.parseInt(str);
		String str2 = request.getParameter("problem_id");
		int problem_id = Integer.parseInt(str2);

		int problem_ispass = 0;

		Problem problem = new Problem();
		problem.setWorker_id(worker_id);
		problem.setProblem_id(problem_id);
		problem.setProblem_ispass(problem_ispass);

		System.out.println("6666666666666666666666666666" + problem);

		// 将员工的状态改为忙碌
		Worker worker = new Worker();
		worker.setWorker_id(worker_id);
		worker.setWorker_state(1);
		WorkerService workerService = new WorkerService();
		workerService.setWorker_state(worker);

		int n = problemService.ProblemSetWorkerId(problem);

		out.print(n);
	}

	public JSONArray getArrByList(List<Problem> list) {

		JSONArray arr = new JSONArray();
		JSONObject json;// {}
		for (Problem problem : list) {
			json = new JSONObject();

			json.put("problem_id", problem.getProblem_id());
			json.put("problem_title", problem.getProblem_title());
			json.put("problem_content", problem.getProblem_content());
			json.put("problem_addcontent", problem.getProblem_addcontent());
			json.put("problem_address", problem.getProblem_address());
			json.put("problem_picture", problem.getProblem_picture());
			json.put("problem_bickunm", problem.getProblem_bickunm());
			json.put("problem_picture2", problem.getProblem_picture2());

			// 时间
			String problem_time = problem.getProblem_time().toString();
			json.put("problem_time", problem_time);

			// 审核状态
			String problem_isauditor = "未审核";
			int isauditor = problem.getProblem_isauditor();
			if (isauditor == -1) {
				// 没过
				problem_isauditor = "未通过";
			} else if (isauditor == 0) {
				// 未审
				problem_isauditor = "未审核";
			} else {
				// 通过
				problem_isauditor = "已通过";
			}
			json.put("problem_isauditor", problem_isauditor);

			// 处理状态
			String problem_ispass = "未处理";
			int ispass = problem.getProblem_ispass();
			if (ispass == -1) {
				// 未处理
				problem_ispass = "未处理";
			} else if (ispass == 0) {
				// 处理中
				problem_ispass = "处理中";
			} else {
				problem_ispass = "已处理";
			}
			json.put("problem_ispass", problem_ispass);

			// 用户姓名
			String problem_userName;
			int users_id = problem.getUsers_id();
			UsersService usersService = new UsersService();
			problem_userName = usersService.selectNameById(users_id);
			json.put("users_name", problem_userName);

			// 员工姓名
			String prolem_workerName;
			int worker_id = problem.getWorker_id();
			WorkerService workerService = new WorkerService();
			prolem_workerName = workerService.selectNameById(worker_id);
			json.put("prolem_workerName", prolem_workerName);

			arr.add(json);
		}
		return arr;

	}

	public String toJsonStr(PageBean pageBean) {

		JSONObject obj = new JSONObject();

		/*
		 * private int rowPerPage; // 每页显示记录数 10--2 11--3 private int totalRows;
		 * // 总记录数 private int curPageNo; // 当前页号 private int totalPageCount; //
		 * 总页数
		 */
		obj.put("rowPerPage", pageBean.getRowPerPage());
		obj.put("totalRows", pageBean.getTotalRows());
		obj.put("curPageNo", pageBean.getCurPageNo());
		obj.put("totalPageCount", pageBean.getTotalPageCount());
		JSONArray arr = new JSONArray();
		JSONObject json;// {}
		for (Problem problem : (List<Problem>) pageBean.getPageList()) {
			json = new JSONObject();
			json.put("problem_id", problem.getProblem_id());
			json.put("problem_title", problem.getProblem_title());
			json.put("problem_content", problem.getProblem_content());
			json.put("problem_addcontent", problem.getProblem_addcontent());
			json.put("problem_address", problem.getProblem_address());
			json.put("problem_picture", problem.getProblem_picture());
			json.put("problem_bickunm", problem.getProblem_bickunm());
			json.put("problem_picture2", problem.getProblem_picture2());

			// 时间
			String problem_time = problem.getProblem_time().toString();
			json.put("problem_time", problem_time);

			// 审核状态
			String problem_isauditor = "未审核";
			int isauditor = problem.getProblem_isauditor();
			if (isauditor == -1) {
				// 没过
				problem_isauditor = "未通过";
			} else if (isauditor == 0) {
				// 未审
				problem_isauditor = "未审核";
			} else {
				// 通过
				problem_isauditor = "已通过";
			}
			json.put("problem_isauditor", problem_isauditor);

			// 处理状态
			String problem_ispass = "未处理";
			int ispass = problem.getProblem_ispass();
			if (ispass == -1) {
				// 未处理
				problem_ispass = "未处理";
			} else if (ispass == 0) {
				// 处理中
				problem_ispass = "处理中";
			} else {
				problem_ispass = "已处理";
			}
			json.put("problem_ispass", problem_ispass);

			// 用户姓名
			String problem_userName;
			int users_id = problem.getUsers_id();
			UsersService usersService = new UsersService();
			problem_userName = usersService.selectNameById(users_id);
			json.put("users_name", problem_userName);

			// 员工姓名
			String prolem_workerName;
			int worker_id = problem.getWorker_id();
			WorkerService workerService = new WorkerService();
			prolem_workerName = workerService.selectNameById(worker_id);
			json.put("prolem_workerName", prolem_workerName);

			arr.add(json);
		}
		obj.put("dataList", arr);
		return obj.toJSONString();

	}

}
